package com.qf.main.pharmacy.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.base.ex.AuditException;
import com.qf.common.base.ex.PhoneCodeException;
import com.qf.common.base.ex.ServiceException;
import com.qf.common.base.result.ResultStatus;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.common.oss.utils.FileUtils;
import com.qf.main.pharmacy.common.qo.PharmacyQO;
import com.qf.main.pharmacy.common.vo.PharmacyInventoryVo;
import com.qf.main.pharmacy.common.vo.PharmacyVO;
import com.qf.main.pharmacy.entity.Pharmacy;
import com.qf.main.pharmacy.entity.PharmacyInventory;
import com.qf.main.pharmacy.mapper.PharmacyInventoryMapper;
import com.qf.main.pharmacy.mapper.PharmacyMapper;
import com.qf.main.pharmacy.service.PharmacyService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author chengqiang
 * @Create 2024-02-04-12:39
 * @Description:
 */
@Service
public class PharmacyServiceImpl implements PharmacyService {
    @Resource
    private PharmacyInventoryMapper pharmacyInventoryMapper;
    @Resource
    private PharmacyMapper pharmacyMapper;
    @Resource
    private FileUtils fileUtils;

    @Override
    public PageInfo<PharmacyVO> listPharmacy(Integer pageNum, Integer pageSize,String pharmacyName,Integer priceSort,Integer dateSort,Integer types,Integer delStatus) {
        String priceSortStr;
        String dateSortStr;
        if(priceSort==null){
            priceSortStr = null;
        }else {
            if(priceSort==0){
                priceSortStr="asc";
            }else if(priceSort==1){
                priceSortStr="desc";
            } else {
                priceSortStr = null;
            }
        }
        if(dateSort==null){
            dateSortStr = null;
        }else {
            if(dateSort==0){
                dateSortStr="asc";
            }else if(dateSort==1){
                dateSortStr="desc";
            } else {
                dateSortStr = null;

            }
        }


        PageInfo<PharmacyVO> PageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> CommonBeanutils.convertTo(pharmacyMapper.selectAllPharmacy(pharmacyName, priceSortStr, dateSortStr,types,delStatus),PharmacyVO::new));

        return PageInfo;
    }

    @Override
    public PageInfo<PharmacyVO> listTypesPharmacy(Integer pageNum, Integer pageSize, int Types, Integer delStatus) {
        QueryWrapper<Pharmacy> qw = new QueryWrapper<>();
        qw.eq(Pharmacy.COL_PHARMACY_TYPES,Types);
        qw.eq(delStatus!=null,Pharmacy.COL_DEL_STATUS,delStatus);
        PageInfo<PharmacyVO> PageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> CommonBeanutils.convertTo(pharmacyMapper.selectList(qw),PharmacyVO::new));
        return PageInfo;
    }

    @Override
    public PageInfo<PharmacyVO> listPharmacyByExamine(Integer pageNum, Integer pageSize,Integer examineStatus) {
        QueryWrapper<Pharmacy> qw = new QueryWrapper<>();
        qw.eq(Pharmacy.COL_EXAMINE_STATUS,examineStatus);
        PageInfo<PharmacyVO> PageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> CommonBeanutils.convertTo(pharmacyMapper.selectList(qw),PharmacyVO::new));
        return PageInfo;
    }

    @Override
    public PharmacyVO getPharmacy(Integer id) {
        QueryWrapper<Pharmacy> qw = new QueryWrapper<>();
        qw.eq(Pharmacy.COL_PHARMACY_ID,id);
        Pharmacy pharmacy = pharmacyMapper.selectOne(qw);
        PharmacyVO pharmacyVO = CommonBeanutils.convertTo(pharmacy, PharmacyVO::new);

        QueryWrapper<PharmacyInventory> qw1 = new QueryWrapper<>();
        qw1.eq(PharmacyInventory.COL_PHARMACY_ID,id);
        PharmacyInventory pharmacyInventory = pharmacyInventoryMapper.selectOne(qw1);
        if(pharmacyInventory.getPharmacyInventory()!=null){
            pharmacyVO.setPharmacyInventory(pharmacyInventory.getPharmacyInventory());
        }


        return pharmacyVO;
    }


    @Override
    @Transactional
    public Integer savePharmacy(PharmacyQO pharmacyQO) {
        QueryWrapper<Pharmacy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Pharmacy.COL_PHARMACY_ID,pharmacyQO.getPharmacyId());
        if(!ObjectUtils.isEmpty(pharmacyMapper.selectOne(queryWrapper))){
            throw  new ServiceException(ResultStatus.PHARMACY_ID_EXIST_ERROR);
        }

        Pharmacy pharmacy = CommonBeanutils.convertTo(pharmacyQO, Pharmacy::new);
        if (pharmacy != null) {
            pharmacy.setPharmacyPrice(new BigDecimal(pharmacyQO.getPharmacyPrice()));
        }
        PharmacyInventory pharmacyInventory = new PharmacyInventory();
        pharmacyInventory.setPharmacyInventory(pharmacyQO.getInventory());
        pharmacyMapper.insert(pharmacy);
        if (pharmacy != null && pharmacy.getPharmacyId() != null) {
            pharmacyInventory.setPharmacyId(pharmacy.getPharmacyId());
            pharmacyInventory.setPharmacyName(pharmacy.getPharmacyName());
            pharmacyInventoryMapper.insert(pharmacyInventory);
        }
        return 1;
    }

    @Override
    public Integer modifyPharmacy(PharmacyQO pharmacyQO) {
        Pharmacy pharmacy = CommonBeanutils.convertTo(pharmacyQO, Pharmacy::new);
        if (pharmacy != null && (pharmacy.getDelStatus() != null && pharmacy.getDelStatus() == 0)) {
            if (pharmacy.getExamineStatus() != 1) {
                throw new AuditException(ResultStatus.AUIDt_NOT);
            }
        }
        pharmacyMapper.updateById(pharmacy);
        // PharmacyInventory pharmacyInventory = CommonBeanutils.convertTo(pharmacyQO, PharmacyInventory::new);
        // pharmacyInventoryMapper.updateById(pharmacyInventory);
        return 1;
    }

    @Override
    public Integer removePharmacy(Integer id) {
        pharmacyMapper.deleteById(id);

        return 1;
    }

    @Override
    public String uploadImage(MultipartFile multipartFile) {
        String file;
        try {
            file = fileUtils.uploadImage(multipartFile,"home"+"/"+"pharmacy");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return file;
    }

    @Override
    public PageInfo<PharmacyInventoryVo> listInventory(Integer pageNum, Integer pageSize,Integer id) {
        QueryWrapper<PharmacyInventory> qw = new QueryWrapper<>();
        qw.eq(id!=null,PharmacyInventory.COL_PHARMACY_ID,id);
        PageInfo<PharmacyInventoryVo> PageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(
                () -> CommonBeanutils.convertTo(pharmacyInventoryMapper.selectList(qw),PharmacyInventoryVo::new));
        return PageInfo;
    }

    @Override
    public Integer reduceInventory(Integer orderMallId, Integer num) {
        QueryWrapper<PharmacyInventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(PharmacyInventory.COL_PHARMACY_ID,orderMallId);
        PharmacyInventory pharmacyInventory = new PharmacyInventory();

        PharmacyInventory pharmacyInventory1 = pharmacyInventoryMapper.selectOne(queryWrapper);
        pharmacyInventory.setInventoryId(pharmacyInventory1.getInventoryId()-num);

        return pharmacyInventoryMapper.update(pharmacyInventory,queryWrapper);
    }


}
